var carorder_collection = db.collection("carorder");
(function() {
	// 构造函数
	var _con = function(info, admin) {
        var self = this;
        var num = 10;
        if (info) {
            // 自有id
            self.info = info;
            self.id = "carmod_" + Math.random().toString(16).substr(2, 2) + (new Date()).getTime().toString(16);
            // 创建时间
            self.createtime = (new Date()).getTime();
            // 模块名称
            /*self.name = info.name;
            // 创建者
            self.creater = admin || {};
            self.creater_id = admin._id;
            // 图标
            //self.preview_img = info.preview_img;
            // 创建时间
            self.createtime = (new Date()).getTime();*/
        };
        if(admin){
            // 创建者
        	self.creater = admin || {};
        }
    };

    // 导出类
    module.exports = _con;

      //租赁房车订单列表
    _con.prototype.list = co.wrap(function*(){
        var self = this;
        if(!self.creater._id){
            return printError("您不是管理员！");
        }
        var data = self.info;
        var page = Math.max(1,parseInt(data.page))-1;
        var status = parseInt(data.status);
        var param = {
            skip: self.num * page,
            limit: self.num,
            sort: {
                createtime: -1
            }
        };
        var data = yield mongo.find(carorder_collection,param);
        var arr = {};
        var user_collection = db.collection("user");
        var touringcar_collection = db.collection("touringcar");
        for(var i=0;i<data.length;i++){
            var car_id = data[i].car_id;
            var user_id = data[i].user_id;
            var user_data = yield mongo.findOne(user_collection,{query:{_id:new ObjectID(user_id)}});
            var car_data = yield mongo.findOne(touringcar_collection,{query:{_id:car_id}});
            data[i].nickname = user_data.nickname;
            data[i].mobile = user_data.mobile_phone;
            data[i].title = car_data.title;
            data[i].rentprice = car_data.rentprice;
           // arr[i].car_id = data[i].car_id;
        }
        return data;
    })

    //租赁房车订单改变状态
    _con.prototype.update = co.wrap(function*(){
        var self = this;
        /*if(!self.creater._id){
            return printError("您不是管理员！");
        }*/
        var data = self.info;
        if(!data._id){
        	return printError('订单不存在！');
        }
        var status = parseInt(data.status);
        var param = {
            query:{
            	_id:data._id
            },
            data:{
            	status:status
            }
        };
        var data = yield mongo.updateOne(carorder_collection,param);
        return data;
    })

    //删除租赁房车订单
    _con.prototype.delete = co.wrap(function*(){
        var self = this;
        /*if(!self.creater._id){
            return printError("您不是管理员！");
        }*/
        var data = self.info;
        if(!data._id){
        	return printError('订单不存在！');
        }
        var status = parseInt(data.status);
        var param = {
            query:{
            	_id:data._id
            }
        };
        var data = yield mongo.removeOne(carorder_collection,param);
        return data;
    })

     //租赁房车订单数量 
    _con.prototype.count = co.wrap(function*(){
        var self = this;
        /*if(!self.creater._id){
            return printError("您不是管理员！");
        }*/
        var data = self.info;

        var status = parseInt(data.status);
        var param = {};
        if(status){
            param.status = status;
        }
        var data = yield mongo.count(carorder_collection,param);
        return data;
    })
}());